<template>
  <div>
    <el-card shadow="never">
      <el-button :disabled="loading" @click="addAdvertSpace">添加广告位</el-button>
    </el-card>
    <el-card shadow="never" :body-style="{ padding: '0px 0px' }">
      <el-table border :data="spaces" style="width: 100%" v-loading="loading">
        <el-table-column prop="spaceKey" label="spaceKey" width="150" align="center">
        </el-table-column>
        <el-table-column prop="name" label="广告位名称" align="center">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="更新时间"
          align="center"
        >
        </el-table-column>
         <el-table-column
          prop="address"
          label="操作"
          align="center"
          width="180px"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="editAdvertSpace(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAdvertSpacePages } from '@/services/advert-space.ts'
import { dateFormat } from '@/utils/date'
export default Vue.extend({
  name: 'AdvertSpaceList',
  data () {
    return {
      spaces: [],
      loading: false,
      page: 1,
      size: 1000,
      total: 0
    }
  },
  created () {
    this.loadAdverts()
  },
  methods: {
    async loadAdverts () {
      const { data } = await getAdvertSpacePages({
        pageNum: this.page,
        pageSize: this.size
      })

      this.spaces = data.content.map((item: FromServer.AdvertSpaceItem) => {
        return {
          ...item,
          createTime: dateFormat(item.createTime, 'YYYY-MM-DD HH:MM:SS'),
          updateTime: dateFormat(item.updateTime, 'YYYY-MM-DD HH:MM:SS')
        }
      })
      this.total = data.content.length
    },
    addAdvertSpace () {
      this.$router.push({
        name: 'advert-space-create'
      })
    },
    editAdvertSpace (data: FromServer.AdvertSpaceItem) {
      this.$router.push({
        name: 'advert-space-edit',
        params: {
          id: data.id.toString()
        }
      })
    }
  }
})
</script>

<style lang="scss" scoped>
</style>
